package site.beanyon.now.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.beanyon.now.constant.ResponseEnum;
import site.beanyon.now.model.dto.HttpResponse;
import site.beanyon.now.model.po.TimeSpanPO;
import site.beanyon.now.service.TimeSpanService;
import site.beanyon.now.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * 时间区间控制器
 */
@RestController
@RequestMapping("/time-span")
public class TimeSpanController {
    @Autowired
    private TimeSpanService timeSpanService;

    /**
     * 创建时间区间
     *
     * @param params 参数Map
     * @return 是否创建成功
     */
    @PostMapping("/create")
    private HttpResponse create(@RequestBody Map<String, String> params) {
        String cardId = params.get("cardId");
        String startTime = params.get("startTime");

        // 对参数判空
        if (StringUtils.isEmpty(cardId) || StringUtils.isEmpty(startTime)) {
            return new HttpResponse(ResponseEnum.REQUIRED_PARAM_NOT_EXISTS, null);
        }

        // 创建时间卡片
        TimeSpanPO timeSpanPO = new TimeSpanPO(new Date(Long.valueOf(startTime)), null, Integer.valueOf(cardId));
        boolean success = this.timeSpanService.createTimeSpan(timeSpanPO);

        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_UPDATE_TIME, timeSpanPO.getId());
        }

        return new HttpResponse(ResponseEnum.FAILED_UPDATE_TIME, null);
    }

    /**
     * 更新开始时间
     *
     * @return 是否更新成功
     */
    @PostMapping("/update-start-time")
    private HttpResponse updateStartTime(@RequestBody Map<String, String> params) {
        String spanId = params.get("spanId");
        String startTime = params.get("startTime");

        // 对参数判空
        if (StringUtils.isEmpty(spanId) || StringUtils.isEmpty(startTime)) {
            return new HttpResponse(ResponseEnum.REQUIRED_PARAM_NOT_EXISTS, null);
        }

        // 更新开始时间
        boolean success = this.timeSpanService.updateStartTime(Integer.valueOf(spanId), Long.valueOf(startTime));

        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_UPDATE_TIME, null);
        }

        return new HttpResponse(ResponseEnum.FAILED_UPDATE_TIME, null);
    }

    /**
     * 更新结束时间
     *
     * @return 是否更新成功
     */
    @PostMapping("/update-end-time")
    private HttpResponse updateEndTime(@RequestBody Map<String, String> params) {
        String spanId = params.get("spanId");
        String endTime = params.get("endTime");

        // 对参数判空
        if (StringUtils.isEmpty(spanId) || StringUtils.isEmpty(endTime)) {
            return new HttpResponse(ResponseEnum.REQUIRED_PARAM_NOT_EXISTS, null);
        }

        // 更新结束时间
        boolean success = this.timeSpanService.updateEndTime(Integer.valueOf(spanId), Long.valueOf(endTime));

        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_UPDATE_TIME, null);
        }

        return new HttpResponse(ResponseEnum.FAILED_UPDATE_TIME, null);
    }
}
